package top.infopub.mgr.util;


import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import top.infopub.mgr.sys.service.BizlogService;
import top.infopub.mgr.sys.service.VisitlogService;
import top.infopub.model.sys.Bizlog;
import top.infopub.model.sys.Visitlog;
import top.infopub.security.util.IpAddrUtil;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.SpringContext;


/**
 * 访问日志/业务日志 工具类
 * @author Awoke
 * @version 2018年10月15日
 * @see LogUtil
 * @since
 */
public final class LogUtil {

    private static VisitlogService visitlogService = null;

    private static BizlogService bizlogService = null;
    
    static {
        visitlogService = SpringContext.getBean("visitlogServiceImpl");
        bizlogService = SpringContext.getBean("bizlogServiceImpl");
    }

    /**
     * 记录后台业务日志
     * @param logModelType 日志模块类型
     * @param acctionType 日志动作类型
     * @param content 操作内容
     * @param remark 备注
     */
    public static void bizLog(LogModelType logModelType, AcctionType acctionType, String content,
                              String remark) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        if (vo != null) {
            Bizlog bizlog = new Bizlog();
            bizlog.setItype(logModelType.getValue());
            bizlog.setSaction(acctionType.getAction());
            bizlog.setScontent(content);
            bizlog.setSremark(remark);
            bizlog.setSip(SessionUserUtils.getRequestIp());
            bizlog.setSrealName(vo.getRealName());
            bizlog.setSuserId(vo.getId());
            bizlog.setSusername(vo.getUserName());
            bizlog.setToperateDate(new Date());
            bizlogService.insert(bizlog);
        }
    }

    /**
     * 记录访问日志
     * @param request
     */
    public static void visitLog(HttpServletRequest request) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        if (vo != null) {
            Visitlog visitlog = new Visitlog();
            visitlog.setOperateDate(new Date());
            visitlog.setRealName(vo.getRealName());
            visitlog.setUserId(vo.getId());
            visitlog.setType(0);
            visitlog.setIp(IpAddrUtil.getIpAddr(request));
            visitlog.setUsername(vo.getUserName());
            visitlog.setVistPath(getRequestPath(request));
            visitlogService.insert(visitlog);
        }
    }
    
    /**
     * 记录操作日志
     * @param logModelType 日志模块类型
     * @param acctionType 日志动作类型
     * @param content 操作内容
     * @param remark 备注
     *//*
    public static void optLog(LogModelType logModelType, AcctionType acctionType, String content,
                              String remark) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        if (vo != null) {
            OptLog optLog = new OptLog();
            optLog.setItype(logModelType.getValue());
            optLog.setSaction(acctionType.getAction());
            optLog.setScontent(content);
            optLog.setSremark(remark);
            optLog.setSip(SessionUserUtils.getRequestIp());
            optLog.setSrealName(vo.getRealName());
            optLog.setSuserId(vo.getId());
            optLog.setSusername(vo.getUserName());
            optLog.setToperateDate(new Date());
            optlogService.insert(optLog);
        }
    }*/

    /**
     * 获取请求路径
     * 
     * @param request
     * @return
     */
    public static String getRequestPath(HttpServletRequest request) {
        String rtnUrl = "";// 返回路径
        String reqUrl = request.getRequestURI();
        if (StringUtils.isNotBlank(reqUrl)) {
            rtnUrl = reqUrl.replace(request.getContextPath(), "").trim();
        }
        return rtnUrl;
    }

    /**
     * 日志动作
     * @version 1.0
     */
    public static enum AcctionType {
        /**
         * 添加
         */
        ADD("add"),
        /**
         * 修改
         */
        UPDATE("update"),
        /**
         * 删除
         */
        DEL("del"),
        /**
         * 下载/导出
         */
        DOWNLOAD("download"),
        /**
         * 查看
         */
        VIEW("view"),
        /**
         * 其他
         */
        OTHER("other");

        private String action;

        // 构造方法
        private AcctionType(String action) {
            this.action = action;
        }

        public String getAction() {
            return action;
        }

    }

    /**
     * 日志模块类型
     * @version 1.0
     */
    public static enum LogModelType {
        /**
         * 系统日志
         */
        SYSTEM(1),
        /**
         * 消息中心
         */
        MESSAGE(2),
        /**
         * 调度日志
         */
        QRTZJOB(3),
        /**
         * 项目日志
         */
        PROJECT(5),
        /**
         * 其他日志
         */
        OTHER(9);

        private int value;

        // 构造方法
        private LogModelType(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }
    }

    public static void main(String[] args) {
        LogUtil.bizLog(LogUtil.LogModelType.SYSTEM, LogUtil.AcctionType.ADD, "内容", "");
    }

}
